$(function() {
	
	$("#slider2").slider({
		value : 0,
		min : 0,
		max : 360,
		step : 1,
		animate : true,
		slide : function(event, ui) {
			$("#a2").val((ui.value) + "개월");
		}
	});
	$("#a2").val($("#slider2").slider("value") + "개월");


});

function doCalc() {
	var AddRowCount = 0;

	var cost = $("#s1").val().replace(/[^0-9]/gi, "");
	var term = $("#s2").val().replace(/[^0-9]/gi, "");
	var interestRate = $("#s3").val().replace(/[^0-9\.]/gi, "");
	var repaymentType = $("#s4").val();
	var ct_no = $("#s5").val(); /* 계약번호 */
	var sa_no = $("#s6").val(); /* 사원번호 */
	var pd_cs = $("#s7").val(); /* 대출시작일 */
	var pd_ce = $("#s8").val(); /* 대출종료일 */
	var repaymentCycle = 1;

	var col = "";
	var row = new Object();

	var totalInterest = 0;
	var avgInterest = 0;
	var totalRepayments = 0;
	var avgRepayments = 0;
	var repaymentTypeKor = "";

	var rest = cost;

	var principalSumTotal = 0;

	var tmpRepaymentCycle = 0;
	var repaymentCnt = (Math.floor(term / repaymentCycle) == term
			/ repaymentCycle) ? Math.round(Math.floor(term / repaymentCycle))
			: Math.round(Math.floor(term / repaymentCycle)) + 1;

	if (repaymentType == '0') {
		repaymentTypeKor = "원리금균등상환";
		var repayments = (cost * (interestRate / 1200) * repaymentCycle)
				/ (1 - 1 / (Math.pow((1 + (interestRate / 1200)
						* repaymentCycle), term / repaymentCycle)));

		for (i = 0; i < repaymentCnt; i++) {
			row[i] = new Object();
			row[i]["repayments"] = repayments;
			row[i]["cnt"] = i + 1;

			tmpRepaymentCycle += repaymentCycle;

			if (tmpRepaymentCycle > term) {
				row[i]["interest"] = rest * (interestRate / 1200)
						* (term - (tmpRepaymentCycle - repaymentCycle));

				totalInterest += row[i]["interest"];

				row[i]["repayments"] = rest + row[i]["interest"];
			} else {
				row[i]["interest"] = rest * (interestRate / 1200)
						* repaymentCycle;

				totalInterest += row[i]["interest"];
			}

			row[i]["principalSum"] = row[i]["repayments"] - row[i]["interest"];

			row[i]["principalSumTotal"] = principalSumTotal += row[i]["principalSum"];

			row[i]["rest"] = rest -= row[i]["principalSum"];

			totalRepayments += row[i]["repayments"];
		}

	} else if (repaymentType == '1') {

		repaymentTypeKor = "원금균등상환";
		for (i = 0; i < repaymentCnt; i++) {
			row[i] = new Object();
			tmpRepaymentCycle += repaymentCycle;
			row[i]["cnt"] = i + 1;

			if (tmpRepaymentCycle > term) {

				row[i]["interest"] = rest * (interestRate / 1200)
						* (term - (tmpRepaymentCycle - repaymentCycle));

				totalInterest += row[i]["interest"];

				row[i]["repayments"] = rest + row[i]["interest"];
			} else {

				row[i]["interest"] = rest * (interestRate / 1200)
						* repaymentCycle;

				totalInterest += row[i]["interest"];
			}

			if (repaymentCnt != i + 1) {

				row[i]["principalSum"] = cost / (term / repaymentCycle);
			} else {
				row[i]["principalSum"] = rest;
			}

			row[i]["repayments"] = row[i]["principalSum"] + row[i]["interest"];

			row[i]["rest"] = rest -= row[i]["principalSum"];

			row[i]["principalSumTotal"] = principalSumTotal += row[i]["principalSum"];

			totalRepayments += row[i]["repayments"];

		}

	} else if (repaymentType == '2') {
		repaymentTypeKor = "원금만기일시상환";

		for (i = 0; i < repaymentCnt; i++) {
			row[i] = new Object();
			row[i]["cnt"] = i + 1;

			row[i]["interest"] = cost * (interestRate / 1200) * repaymentCycle;

			totalInterest = row[i]["interest"] * repaymentCnt;
			if (i + 1 == repaymentCnt) {
				row[i]["principalSum"] = cost;

				row[i]["repayments"] = Number(row[i]["principalSum"])
						+ Number(row[i]["interest"]);

			} else {
				row[i]["principalSum"] = 0;
				row[i]["repayments"] = Number(row[i]["principalSum"])
						+ Number(row[i]["interest"]);
			}

			row[i]["rest"] = rest -= row[i]["principalSum"];

			row[i]["principalSumTotal"] = principalSumTotal += row[i]["principalSum"];

			totalRepayments += row[i]["repayments"];
		}
	}

	avgInterest = Math.abs(Math.round(totalInterest / repaymentCnt));
	totalInterest = Math.abs(Math.round(totalInterest));
	avgRepayments = Math.abs(Math.round(totalRepayments / repaymentCnt));
	totalRepayments = Math.abs(Math.round(totalRepayments));

	var table1 = $("#table1 td");
	table1[0].innerHTML = addCommas(cost) + "원";

	table1[1].innerHTML = term + "개월";
	table1[2].innerHTML = interestRate + "%";
	table1[3].innerHTML = repaymentCycle + "개월";
	table1[4].innerHTML = repaymentTypeKor;
	table1[5].innerHTML = addCommas(Math.round(row[0]["repayments"])) + "원";
	table1[6].innerHTML = addCommas(Math.round(totalInterest)) + "원";

	if (repaymentCnt > 0) {
		$("#tbody").remove();
		$("#trbody").remove();
		$("#table2,#table1").addClass('table-bordered');
		$('<tbody id="tbody"></tbody>').appendTo("#table2");
	}

	var innerHtmlString = '<tr><td></td><td></td><td></td><td></td><td></td><td></td></tr>';
	for (i = 0; i < repaymentCnt; i++) {
		$(innerHtmlString).appendTo("#tbody");
	}

	AddRowCount = repaymentCnt;

	var table2 = $("#table2 td");
	var didi = document.getElementById("div1");
	didi.innerHTML = "";
	for (i = 0; i < repaymentCnt; i++) {

		didi.innerHTML += "<input type='TEXT'  name='CS_ST' >";
		didi.innerHTML += "<input type='text'  name='CS_MN' >";
		didi.innerHTML += "<input type='text'  name='CS_OM' >";
		didi.innerHTML += "<input type='text'  name='CS_RM' >";
		didi.innerHTML += "<input type='text'  name='CS_SOM'>";
		didi.innerHTML += "<input type='text'  name='CS_SM' >";
		didi.innerHTML += "<input type='text'  name='CS_REM' >";
		didi.innerHTML += "<input type='text'  name='PD_PRD' >";
		didi.innerHTML += "<input type='text'  name='PD_RATE' >";
		didi.innerHTML += "<input type='text'  name='PD_PBACK'>";
		didi.innerHTML += "<input type='text'  name='CS_SRM' >";
		didi.innerHTML += "<input type='text'  name='CT_NO'>";
		didi.innerHTML += "<input type='text'  name='SA_NO'>";
		didi.innerHTML += "<input type='text'  name='CS_LOAN_START'>";
		didi.innerHTML += "<input type='text'  name='CS_LOAN_END'>";

	}

	var ct = document.getElementsByName('CT_NO');
	var sa = document.getElementsByName('SA_NO');
	var cs = document.getElementsByName('CS_LOAN_START');
	var ce = document.getElementsByName('CS_LOAN_END');
	var mn = document.getElementsByName('CS_MN');
	var st = document.getElementsByName('CS_ST');
	var om = document.getElementsByName('CS_OM');
	var rm = document.getElementsByName('CS_RM');
	var som = document.getElementsByName('CS_SOM');
	var sm = document.getElementsByName('CS_SM');
	var rem = document.getElementsByName('CS_REM');
	var prd = document.getElementsByName('PD_PRD');
	var rate = document.getElementsByName('PD_RATE');
	var pback = document.getElementsByName('PD_PBACK');
	var srm = document.getElementsByName('CS_SRM');
	var p = 0;
	for (i = 0; i < repaymentCnt; i++) {

		table2[(i * 6) + 0].innerHTML = row[i]["cnt"];
		table2[(i * 6) + 1].innerHTML = addCommas(Math
				.round(row[i]["repayments"]))
				+ "원";
		table2[(i * 6) + 2].innerHTML = addCommas(Math
				.round(row[i]["principalSum"]))
				+ "원";
		table2[(i * 6) + 3].innerHTML = addCommas(Math
				.round(row[i]["interest"]))
				+ "원";
		table2[(i * 6) + 4].innerHTML = addCommas(Math
				.round(row[i]["principalSumTotal"]))
				+ "원";
		table2[(i * 6) + 5].innerHTML = addCommas(Math.round(row[i]["rest"]))
				+ "원";

		st[p].value = table2[(p * 6) + 0].innerHTML;

		mn[p].value = table2[(p * 6) + 1].innerHTML;

		om[p].value = table2[p * 6 + 2].innerHTML;

		rm[p].value = table2[p * 6 + 3].innerHTML;
		som[p].value = table2[p * 6 + 4].innerHTML;
		sm[p].value = table2[p * 6 + 5].innerHTML;
		rem[p].value = table1[0].innerHTML;
		prd[p].value = table1[1].innerHTML;
		rate[p].value = table1[2].innerHTML;
		pback[p].value = table1[4].innerHTML;
		srm[p].value = table1[6].innerHTML;
		ct[p].value = ct_no;
		sa[p].value = sa_no;
		cs[p].value = table2[(p * 6) + 0].innerHTML;
		ce[p].value = pd_ce;

		p++;

	}

}